package org.yecq.tradedata.algorithm;

import com.jhhc.baseframework.data.record.SqlOperator;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.yecq.tradedata.bean.FuturesDayKLineBean;

/**
 *
 * @author yecq
 */
@Component
class Utils {

    @Autowired
    private SqlOperator sql;

    // 获取某天之前（包括该天）的K线集合
    public List<FuturesDayKLineBean> getDayKLineList4Futures(String code, String start_date, int number) {
        String stmt = "select * from futures_day_kline where date <= ? and futures_id=(select id from futures where code = ? ) order by date desc limit ?";
        List<Map<String, Object>> list = this.sql.query(stmt, start_date, code, number);
        List<FuturesDayKLineBean> ret = new ArrayList(list.size());
        for (Map<String, Object> item : list) {
            FuturesDayKLineBean bean = new FuturesDayKLineBean();
            bean.setClose(Double.parseDouble(item.get("close") + ""));
            bean.setCode(code);
            bean.setDate(item.get("date") + "");
            bean.setHigh(Double.parseDouble(item.get("high") + ""));
            bean.setLow(Double.parseDouble(item.get("low") + ""));
            bean.setOpen(Double.parseDouble(item.get("open") + ""));
            bean.setVol(Integer.parseInt(item.get("vol") + ""));

            ret.add(bean);
        }
        return ret;
    }
}
